using UnityEngine;
using UnityEditor; 
using System.Collections;
using System.Collections.Generic;


[CustomEditor(typeof(PositionByValueGroupOnUpdate))]  
public class PositionByValueGroupInspector : Editor
{
    public Character character;
    private CharacterAnimatedState state;
    
    PositionByValueGroupOnUpdate action;

    public static float forwardMax = 3.0f;

    public static float upMax = 3.0f;

    public static float rightMax = 3.0f;

    public static float forwardMin = -3.0f;

    public static float upMin = -3.0f;

    public static float rightMin = -3.0f;
    
    void FindReferences()
    {
        state = action.transform.parent.parent.GetComponent<CharacterAnimatedState>();
        character = state.transform.parent.parent.GetComponent<Character>();
    }

    public override void OnInspectorGUI () 
    {  

        action = target as PositionByValueGroupOnUpdate;

        if(character == null)
        {
            FindReferences();
        }

        EditorGUILayout.LabelField("dataLength");       
        action.dataLength = EditorGUILayout.IntField(action.dataLength);

        EditorGUILayout.LabelField("----------------------------------------------------------------------------------");
        forwardMax = EditorGUILayout.Slider("forwardPositionMax",forwardMax,-5,5);

        upMax = EditorGUILayout.Slider("upPositionMax",upMax,-5,5);

        rightMax = EditorGUILayout.Slider("rightPositionMax",rightMax,-5,5);

        forwardMin = EditorGUILayout.Slider("forwardPositionMin",forwardMin,-5,5);

        upMin = EditorGUILayout.Slider("upPositionMin",upMin,-5,5);

        rightMin = EditorGUILayout.Slider("rightPositionMax",rightMin,-5,5);

        EditorGUILayout.Separator();
        EditorGUILayout.LabelField("----------------------------------------------------------------------------------");


        if(action.positionGroup == null )
        {
            action.positionGroup = new PositionData[action.dataLength];
            for(int i=0;i<action.dataLength;i++)
            {
                action.positionGroup[i] = new PositionData();
            }
        }

        if(action.positionGroup.Length < action.dataLength)
        {
            PositionData[] group = action.positionGroup;
            action.positionGroup = new PositionData[action.dataLength];
            for(int i=0;i<action.dataLength;i++)
            {
                action.positionGroup[i] = new PositionData();
            }
            for(int j=0;j<group.Length;j++)
            {
                action.positionGroup[j].frame = group[j].frame;
                action.positionGroup[j].forward = group[j].forward;
                action.positionGroup[j].up = group[j].up;
                action.positionGroup[j].right = group[j].right;
            }
        }

        if(action.positionGroup.Length > action.dataLength)
        {
            PositionData[] group = action.positionGroup;
            action.positionGroup = new PositionData[action.dataLength];
            for(int i=0;i<action.dataLength;i++)
            {
                action.positionGroup[i] = new PositionData();
            }
            for(int j=0;j<action.dataLength;j++)
            {
                action.positionGroup[j].frame = group[j].frame;
                action.positionGroup[j].forward = group[j].forward;
                action.positionGroup[j].up = group[j].up;
                action.positionGroup[j].right = group[j].right;
            }
        }

        for(int i=0;i<action.positionGroup.Length;i++)
        {
            int frameMax = 500;
            if(state.frames != 0) 
            {
                frameMax = state.frames;
            }
            if(i == 0)
            {
                //Debug.LogError(action.positionGroup[i].frame);
                //Debug.LogError(state.frames);

                action.positionGroup[i].frame = EditorGUILayout.IntSlider("frame : " + "0 ~ " + action.positionGroup[i].frame, action.positionGroup[i].frame,0,frameMax);
            }
            else
            {
                action.positionGroup[i].frame = EditorGUILayout.IntSlider("frame :" + action.positionGroup[i-1].frame + " ~ " + action.positionGroup[i].frame,action.positionGroup[i].frame,action.positionGroup[i-1].frame,frameMax);
            }

            EditorGUILayout.LabelField("forwardPosition");
            action.positionGroup[i].forward = EditorGUILayout.Slider(action.positionGroup[i].forward, forwardMin,forwardMax);

            EditorGUILayout.LabelField("upPosition");
            action.positionGroup[i].up = EditorGUILayout.Slider(action.positionGroup[i].up, upMin,upMax);

            EditorGUILayout.LabelField("rightPosition");
            action.positionGroup[i].right = EditorGUILayout.Slider(action.positionGroup[i].right, rightMin,rightMax);

            EditorGUILayout.Space();
            EditorGUILayout.Space();
        }
    }
    

}


